package vip.web3.api.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import vip.web3.api.entity.biz.RecordEntity;
import vip.web3.api.vo.biz.RecordVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * aut-horlilei
 */
@Mapper
public interface RecordMapper {
    RecordMapper INSTANCE = Mappers.getMapper(RecordMapper.class);


    /**
     * toUserInfoVo
     *
     * @param recordEntity recordEntity
     * @return RecordVo
     */
    @Mapping(target = "amount", ignore = true)
    @Mapping(target = "fee", ignore = true)
    RecordVo toRecordVo(RecordEntity recordEntity);

    /**
     * mergeUserInfoVo
     *
     * @param recordEntity recordEntity
     * @return UserInfoVo
     */
    default RecordVo convertRecordVo(RecordEntity recordEntity) {

        RecordVo recordVo = this.toRecordVo(recordEntity);
        if (Objects.nonNull(recordEntity)) {
            BigDecimal amount = Objects.isNull(recordEntity.getAmount()) ? BigDecimal.ZERO : recordEntity.getAmount();
            BigDecimal fee = Objects.isNull(recordEntity.getFee()) ? BigDecimal.ZERO : recordEntity.getFee();

            recordVo.setAmount(amount.stripTrailingZeros().toPlainString());
            recordVo.setFee(fee.stripTrailingZeros().toPlainString());
        }

        return recordVo;
    }

    default List<RecordVo> convertRecordVoList(List<RecordEntity> records) {
        if (records == null) {
            return null;
        }

        List<RecordVo> list = new ArrayList<RecordVo>(records.size());
        for (RecordEntity recordEntity : records) {
            list.add(this.convertRecordVo(recordEntity));
        }

        return list;
    }
}
